package com.gsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gsc.constants.SystemConstants;
import com.gsc.domain.ResponseResult;
import com.gsc.domain.entity.Link;
import com.gsc.domain.vo.LinkDateVo;
import com.gsc.domain.vo.PageVo;
import com.gsc.mapper.LinkMapper;
import com.gsc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import com.gsc.service.LinkService;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-01-31 16:05:26
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    /**
     * 查询审核通过的友链
     * @return List<LinkDateVo>
     */
    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通多的友联
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        //构造状态为审核通过的
        queryWrapper.eq(Link::getStatus,SystemConstants.LINK_STATUS_NORMAL);
        List<Link> links = this.list(queryWrapper);
        //转成vo
        List<LinkDateVo> listVos = BeanCopyUtils.copyBeanList(links, LinkDateVo.class);
        //封装返回
        return ResponseResult.okResult(listVos);
    }

    @Override
    public PageVo selectPageLink(Integer pageNum, Integer pageSize, Link link) {

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(link.getStatus()),Link::getStatus,link.getStatus());
        queryWrapper.like(StringUtils.hasText(link.getName()),Link::getName,link.getName());

        //分页查询
        Page page = new Page(pageNum,pageSize);
        page(page,queryWrapper);

        //封装vo
        return new PageVo(page.getRecords(),page.getTotal());
    }
}
